package com.RedRiver.hospitalsystem.record_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.patient_service.client.PatientClient;
import com.RedRiver.hospitalsystem.patient_service.client.param.PatientInfo;
import com.RedRiver.hospitalsystem.record_service.common.Record_serviceResultCode;
import com.RedRiver.hospitalsystem.record_service.common.exception.RecordsException;
import com.RedRiver.hospitalsystem.record_service.form.AddResultForm;
import com.RedRiver.hospitalsystem.record_service.form.CreateRecordForm;
import com.RedRiver.hospitalsystem.record_service.info.SearchRecordByIdInfo;
import com.RedRiver.hospitalsystem.record_service.infra.mapper.RecordsMapper;
import com.RedRiver.hospitalsystem.record_service.infra.model.Records;
import com.RedRiver.hospitalsystem.record_service.service.RecordsService;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class RecordsServiceImpl extends ServiceImpl<RecordsMapper, Records> implements RecordsService {

    @Resource
    private PatientClient patientClient;

    @Override
    public Result<?> addRecord(CreateRecordForm recordForm) {
        Result<PatientInfo> patient = patientClient.findAllPatientById(recordForm.getPatientId());
        if (patient.getData()!=null) {
            Records records = BeanUtil.copyProperties(recordForm, Records.class);
            int insert = baseMapper.insert(records);
            if (insert>0) {
                return Result.getInstance().setMsg("添加病历成功,病历id为："+records.getRecordsId()+",请尽快填写处方");
            }throw new RecordsException(Record_serviceResultCode.CREATE_RECORD_ERROR);
        }throw new RecordsException(Record_serviceResultCode.PATIENT_NOT_EXISTS_ERROR);

    }

    @Override
    public List<SearchRecordByIdInfo> findRecord(Integer id) {
        Result<PatientInfo> patient = patientClient.findAllPatientById(id);
        if (patient.getData()!=null) {
            List<Records> recordsList = baseMapper.selectList(new QueryWrapper<Records>().eq("patient_id", id));
            if (recordsList.size()!=0) {
                List<SearchRecordByIdInfo> list= BeanUtil.copyToList(recordsList, SearchRecordByIdInfo.class);
            }
            throw new RecordsException(Record_serviceResultCode.NO_RECORD_ID_ERROR);
        }throw new RecordsException(Record_serviceResultCode.PATIENT_NOT_EXISTS_ERROR);
    }

    @Override
    public Result<?> addRecordResult(AddResultForm resultForm) {
        Records records = baseMapper.selectById(resultForm.getRecordsId());
        if (records!=null) {
            records.setRecordsResult(resultForm.getRecordsResult());
            int i = baseMapper.updateById(records);
            if (i>0) {
                return  Result.getInstance().setMsg("添加结果成功");
            }
            throw new RecordsException(Record_serviceResultCode.ADDRECORD_ERROR);
        }
        throw new RecordsException(Record_serviceResultCode.RECORDS_NOT_EXISTS_ERROR);
    }
}
